/*
 * Copyright (c) 2020 the original author or authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.donny.ni.cface.core.controller;

import com.donny.ni.cface.core.param.FacesDetectionResponseDto;
import com.donny.ni.cface.core.param.ProcessImageParams;
import com.donny.ni.cface.core.service.FaceProcessService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
@Validated
public class DetectionController {

    private final FaceProcessService detectionService;

    @PostMapping(value = "/detection/detect", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public FacesDetectionResponseDto detect(
            @RequestParam final MultipartFile file,
            @RequestParam(defaultValue = "0", required = false) final Integer limit,
            @RequestParam(value = "det_prob_threshold", required = false) final Double detProbThreshold,
            @RequestParam(value = "face_plugins", required = false, defaultValue = "") final String facePlugins,
            @RequestParam(value = "status", required = false, defaultValue = "false") final Boolean status) {
        var processImageParams = ProcessImageParams
                .builder()
                .file(file)
                .limit(limit)
                .detProbThreshold(detProbThreshold)
                .facePlugins(facePlugins)
                .status(status)
                .build();

        return (FacesDetectionResponseDto) detectionService.processImage(processImageParams);
    }

}
